package com.qys.wallet.pay.hyperbc;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSONObject;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.qys.wallet.pay.hyperbc.dto.resp.CallBackDTO;
import com.qys.wallet.pay.hyperbc.util.RsaSignerUtils;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SignatureException;
import java.security.spec.InvalidKeySpecException;
import java.util.HashMap;
import java.util.Map;


@Tag(name = "回调-Hyperbc", description = "回调-Hyperbc")
@Slf4j
@RestController
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@RequestMapping("/hypay")
public class HyRpcCallbackController {

    private final HyperbcCallBackService callBackService;

    private static final String pubkey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAlcTQaOfwdWZS0CE92FAB" +
            "MDN4IcSgnw3uUn/i1iv2/eSUqc8kjmHV+RizH/uJoVRUDNGoZrltgoZa4gmZZH63" +
            "p6QTkKKTawTpJn5rozfmjDPSwMPwcV27O7QYRZq/QzNeyAJtHDA8W3I4CzCoX235" +
            "OXCDlnpNN/DrlOtMMQnpOeloQrmP+Jonl+mwMHQis+VA3he1Pd4xXR5aZj6Mav7I" +
            "2vd6EkvAVB1W01mpPySYYm1cJ75X04U82/8+9yf+Dr2V479sQgaLVO/dOwGUuDWJ" +
            "Ut6EewprjURKXBNgh5ct70x9sAnubXQTb9OnoafMtwAHOOMx16BC9ayhpmxoEXOP" +
            "PQIDAQAB";

    private static final String priKey =
            "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCVxNBo5/B1ZlLQ" +
            "IT3YUAEwM3ghxKCfDe5Sf+LWK/b95JSpzySOYdX5GLMf+4mhVFQM0ahmuW2Chlri" +
            "CZlkfrenpBOQopNrBOkmfmujN+aMM9LAw/BxXbs7tBhFmr9DM17IAm0cMDxbcjgL" +
            "MKhfbfk5cIOWek038OuU60wxCek56WhCuY/4mieX6bAwdCKz5UDeF7U93jFdHlpm" +
            "Poxq/sja93oSS8BUHVbTWak/JJhibVwnvlfThTzb/z73J/4OvZXjv2xCBotU7907" +
            "AZS4NYlS3oR7CmuNREpcE2CHly3vTH2wCe5tdBNv06ehp8y3AAc44zHXoEL1rKGm" +
            "bGgRc489AgMBAAECggEAApre2x92cO+CLwkpx7dNUsoAyjZe0TvPTBNzkRd7rAFy" +
            "GoM2I6nZxgOv3eiknxZvgQRgIiursKXNZkZ8RG5PaHTAofcWveyvpEVC2J7SY9h+" +
            "gm7S1RrBwaSWXxbCQkhEjkSiIprhYEY1nL0DnxrmrYdikPDFb6mN/dQBfVrd4FPc" +
            "11dJ+I64GpBVzEhoszTcKkNBakw89uo+MZgPryjLXWSSaq1KqnWx3Dz4cyhKl8SJ" +
            "63804hMA1Pqmf+VoRybv1nKYFdPTZpDPdgqrfPoP3pSGjqIcfyLAzqZHgi28nfQ5" +
            "hwbe/NYvbyafdbFx6/YzLouz9FBn/2TIYgKmspCkEQKBgQC1Sb/mX73Sug/mJqsg" +
            "/3lfuDeA+XBApTy+WzxBajWG6g2s3QEHbnUf4oBoK6A7VVFnqm+3lp4cZ/djsUA3" +
            "TIJ3vkw3zI1tfz/QQz0x4nDnyi69Q8H2iDLTO8r6l5YF/eeQKtPkXUrN5gtNnQi0" +
            "oqZ8aaXWhamXcUIQsCM8ciheGQKBgQDTfbfnCDXQNdr3MouK65wl+hmt7vEgaEm+" +
            "70LGbZ965c+LwRd8VqcWUwGRt5B1BfAenOZVv6gyoDWyxUlF27ex+BPXI/4bbwG5" +
            "i/YURB2wgQeO/XXiFpjJyzbpqezPWQdwWYfImJPuhwHozZkYKvXRhRYfVKrZSXIY" +
            "UZIF0lUWxQKBgQCplOBu5taRnb0rpLZl+rUWBiPdgPgxcKBezMeTxX9mweCi+GVz" +
            "By+G8QD+8PhFKG0q1PMGQF2M4vA3EGKql+ZfjuKLO8x8RhFyhsNiu5i0KpwILa7u" +
            "J7+rev5H5a7P0hBf+sAVmfNUocGnC6NOHeww6z7XalcXNDoBlFKRw5PhWQKBgFse" +
            "czlq5CSMz4ZplI2J7YC/PIkfxWu9AujslMyBNpDwqnb//mpnux13LdnmSKrioA3Q" +
            "c7zGEmWdeQVI0sTyYTagN4wdycFwFkIm9yLMJi6BqQwGT9/U9IccstABkjHTncze" +
            "Bg57DmFacYwn6Kns+HQI7vsYs3NinXG000EG2foxAoGAVb3ixEujKiM/qISHucwC" +
            "NxMb6zduJMf1Xdmdh1xm6tCJ66cMsGoVK4SKT9TrwDWciXBxw323q1ZdGb3oEh/z" +
            "eO3C5xJESYGEhQUQw7S+TYMQ71x2U0oA9N/NebWZEM7pNNx8Q8wgKVtFWe4yPRSM" +
            "ZJ+2r6hBLEs+ULzRa5kQk+8=";

    /**
     * 充值回调
     */
    @PostMapping("/recharge/notify")
    public String rechargeCallback(@RequestBody CallBackDTO dto) {
        log.info("接受到充值回调dto:" + JSONObject.toJSONString(dto));
        //TODO 业务处理
        log.info("=====收到充币回调通知======");
        return callBackService.handleCallback(dto,1);
    }


    /**
     * 提现回调
     */
    @PostMapping("/withdrawal/notify")
    public String withdrawalCallback(@RequestBody CallBackDTO dto) {
        log.info("接受到提现回调dto:" + JSONObject.toJSONString(dto));
        //TODO 业务处理
        log.info("=====收到提现回调通知======");
        return callBackService.handleCallback(dto,2);

    }


    /**
     * 风控提现回调
     */
    @PostMapping("/withdrawal/re_notify")
    public Map<String, Object> riskControlCallback(@RequestBody CallBackDTO dto) {
        log.info("接受到提现回调dto:" + JSONObject.toJSONString(dto));
        //TODO 业务处理
        log.info("=====收到风控回调通知======");
       // return callBackService.riskControlCallback(dto);
        return null;
    }



    public static void main(String[] args) throws NoSuchAlgorithmException, InvalidKeySpecException, SignatureException, InvalidKeyException {
        Map<String, Object> data = new HashMap<>();
        data.put("order_id", "2020010211153423123456");
        data.put("coin", "eth");
        data.put("chain", "eth");
        data.put("address", "this is test address");
        data.put("txid", "this is txid");
        data.put("amount", "1");
        data.put("fee", "0");
        data.put("total", "1");
        data.put("status", "1");
        data.put("confirm_count", "3");
        data.put("time", "1650011196");
        data.put("type", "1");
        Map<String, Object> params = new HashMap<>(2);
        params.put("data", data);
        params.put("sign", RsaSignerUtils.genSign(data, priKey));
        Gson gson = new GsonBuilder().create();
        System.out.println("params = " + gson.toJson(params));
        String url = "http://7c7rfw.natappfree.cc/hypayV2/recharge/notify";
        String res = HttpUtil.post(url, JSONObject.toJSONString(params));
        System.out.println(res);

    }


}
